package cn.edu.pzhu.service.imp;

import java.util.List;

import org.springframework.dao.DataAccessException;

import cn.edu.pzhu.dao.FileMsgDAO;
import cn.edu.pzhu.dao.imp.FileMsgDAOImp;
import cn.edu.pzhu.pojo.FileMsg;
import cn.edu.pzhu.pojo.Msg;
import cn.edu.pzhu.service.FileMsgService;

public class FileMsgServiceImp implements FileMsgService{
	
	private FileMsgDAO dao = new FileMsgDAOImp();
	
	@Override
	public List<FileMsg> showFileList() {
		try {
			return dao.selectAll();
	    } catch (DataAccessException e) {
	        return null;
	    }
	}

	@Override
	public Msg upload(FileMsg filemsg) {
		int n = dao.insert(filemsg);
		if(n>0) {
			//插入账号成功
			return new Msg(true, "上传成功！");
		}
		else {
			return new Msg(false, "上传失败！");
		}
	}

	@Override
	public List<FileMsg> showFileListByUsername(String username) {
		if (username == null || username.trim().isEmpty()) {
	        throw new IllegalArgumentException("用户名为空！");
	    }
	    try {
	        return dao.selectByUsername(username);
	    } catch (DataAccessException e) {
	        return null;
	    }
	}

	@Override
	public List<FileMsg> selectByCategories(String categories) {
		if (categories == null || categories.trim().isEmpty()) {
	        throw new IllegalArgumentException("分类条目为空！");
	    }
	    try {
	        return dao.selectByCategories(categories);
	    } catch (DataAccessException e) {
	        return null;
	    }
	}

	@Override
	public Msg delete(String filename) {
		int n = dao.destroy(filename);
		if (n > 0) {
			return new Msg(true,"删除成功");
		}
		return new Msg(false,"删除失败");
	}

	@Override
	public List<FileMsg> showFileListByKeyword(String keyword) {
		if (keyword == null || keyword.trim().isEmpty()) {
	        throw new IllegalArgumentException("密码为空！");
	    }
	    try {
	        return dao.selectByKeyword(keyword);
	    } catch (DataAccessException e) {
	        return null;
	    }
	}

	@Override
	public String findFilePath(String filename, String username) {
		if (filename == null || filename.trim().isEmpty()) {
	        throw new IllegalArgumentException("密码为空！");
	    }
		if (username == null || username.trim().isEmpty()) {
	        throw new IllegalArgumentException("密码为空！");
	    }
		try {
			return dao.selectFilePath(filename,username);
	    } catch (DataAccessException e) {
	        return null;
	    }
	}
	
}
